package com.fitness.fitness.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.fitness.convertor.NewsConvertor;
import com.fitness.fitness.mapper.NewsMapper;
import com.fitness.fitness.modal.news.dto.NewsDto;
import com.fitness.fitness.modal.news.entity.News;
import com.fitness.fitness.modal.news.params.NewsPageQueryParams;
import com.fitness.fitness.modal.news.vo.NewsVO;
import com.fitness.fitness.service.INewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    private final NewsConvertor newsConvertor;
    private final ConversionService conversionService;

    @Override
    public Page<NewsVO> getNewsPage(NewsPageQueryParams queryParams) {
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<News> page = new Page<>(pageNum, pageSize);
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StrUtil.isBlank(queryParams.getTitle()),"title",queryParams.getTitle());
        Page<News> retPage = this.baseMapper.selectPage(page,queryWrapper);

        return newsConvertor.entity2vo(retPage);
    }

    @Override
    public boolean addNews(NewsDto newsDto) {
        return this.baseMapper.insert(newsDto) > 0;
    }

    @Override
    public boolean updateNews(NewsDto newsDto) {
        return this.baseMapper.updateById(newsDto) > 0;
    }
}
